<template>
    <div class="login_contain">
        <!-- 登录块   -->
        <div class="login_box">
            <!--   放置图片   -->
            <div class="avater_box">
                <img src="../assets/img/b2.jpg">
            </div>
            <!-- 登录表单 -->
            <!--   :rules="loginRules" 表示检验规则-->
            <el-form ref="LoginForms" :model="LoginForm" label-width="0" class="Login_form"  :rules="LoginRules">
                <!--    用户名    -->
                <el-form-item prop="username">
                    <el-input v-model="LoginForm.username" prefix-icon="el-icon-user"></el-input>
                </el-form-item>
                <!--    密码   -->
                <el-form-item prop="password">
                    <el-input v-model="LoginForm.password" type="password" prefix-icon="el-icon-unlock"></el-input>
                </el-form-item>
                <el-link type="primary" @click="tiao">没有账号，前往注册</el-link>
                <!--    按钮    -->
                <el-form-item class="btns">
                    <!--    提交的和ref的名一样    -->
                    <el-button type="primary" @click="submitForm">登录</el-button>
                    <el-button type="warning" @click="resetForm">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            var validateName  = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入用户名'));
                } else  {
                    if(value!==''){
                        this.$http.get("user/check",{params:{username:this.LoginForm.username}}).then(res=>{
                            if(res.data.code==20001){
                                callback(new Error('用户名不存在'));
                            }
                            else {
                                callback();
                            }
                        })
                    }
                }
            };
            return {
                LoginForm: {
                    username: '',
                    password: '',
                },
                LoginRules: {
                    username: [
                        { validator: validateName, trigger: 'blur' }
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                    ],
                }
            };
        },
        methods: {
            submitForm() {
                 this.$refs.LoginForms.validate(async valid => {
                   if(!valid) return ;
                   const{data:res} = await this.$http.post("user/login",this.LoginForm);
                   if(res.code==20000){
                       //获取当前对象
                       window.sessionStorage.setItem("user",res.data.user);
                       this.$message.success("登录成功");
                        this.$router.push("/main")
                    }
                   else if(res.code=20001){
                       this.$message.error("密码错误");
                   }
                 });
            },
            resetForm() {
                this.$refs.LoginForms.resetFields();
            },
            tiao() {
                //进行页面跳转
                this.$router.push("/regist");
            }
        }
    }
</script>

<style scoped lang="less">
    // 跟节点样式
    .login_contain {
        background-color: aquamarine;
        height: 100%;
    }

    // 登录框
    .login_box {
        width: 450px;
        height: 350px;
        background-color: #fff;
        border-radius: 3px; // 圆角
        position: absolute; // 绝对定位
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%); // 根据自己位置 以自己为长度位移
        // 头像框
        .avater_box {
            width: 130px;
            height: 130px;
            border: 1px solid #EEEEEE;
            border-radius: 50%; // 加圆角
            padding: 10px;
            box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
            position: absolute;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: #FFFFFF;

            img {
                width: 100%;
                height: 100%;
                border-radius: 50%; // 加圆角
                background-color: #EEEEEE;
            }
        }
    }

    .btns {
        display: flex; // 弹性布局
        justify-content: flex-end; // 尾部对齐
    }

    .Login_form {
        position: absolute;
        bottom: 0%;
        width: 100%;
        padding: 0 30px;
        box-sizing: border-box; // 设置边框
    }
</style>
